package cn.xlunzi.games.activity

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.inputmethod.InputMethodManager
import cn.xlunzi.games.R
import cn.xlunzi.games.utils.NumUtil
import cn.xlunzi.games.utils.TT
import kotlinx.android.synthetic.main.activity_guess_num.*
import java.util.*
import kotlin.collections.HashSet

/**
 * Created by SunLW on 2018-04-09.
 */
class GuessNumActivity : Activity() {

    private val sb: StringBuilder = StringBuilder()

    private var count: Int = 0

    private val numList: MutableList<Int> = MutableList(4, { -1 })

    private val setForUnique = HashSet<Int>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_guess_num)

        resetGame()

        tv_title_guess_num.setOnClickListener {
            resetGame()
        }

        et_one.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable) {
                if (!TextUtils.isEmpty(s)) {
                    if (s.length > 1) {
                        et_one.setText(s.subSequence(0, 1))
                        return
                    }
                    et_two.requestFocus()
                }
            }
        })

        et_two.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable) {
                if (!TextUtils.isEmpty(s)) {
                    if (s.length > 1) {
                        et_two.setText(s.subSequence(0, 1))
                        return
                    }
                    et_three.requestFocus()
                }
            }
        })

        et_three.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable) {
                if (!TextUtils.isEmpty(s)) {
                    if (s.length > 1) {
                        et_three.setText(s.subSequence(0, 1))
                        return
                    }
                    et_four.requestFocus()
                }
            }
        })

        et_four.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable) {
                if (!TextUtils.isEmpty(s)) {
                    if (s.length > 1) {
                        et_four.setText(s.subSequence(0, 1))
                        return
                    }
                    et_four.clearFocus()
                    val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                    imm.hideSoftInputFromWindow(window.decorView.windowToken, 0)
                }
            }
        })

        btn_commit.setOnClickListener {
            val numOneStr = et_one.text.toString().trim()
            val numTwoStr = et_two.text.toString().trim()
            val numThreeStr = et_three.text.toString().trim()
            val numFourStr = et_four.text.toString().trim()

            if (TextUtils.isEmpty(numOneStr) || TextUtils.isEmpty(numTwoStr) ||
                    TextUtils.isEmpty(numThreeStr) || TextUtils.isEmpty(numFourStr)) {
                TT.show("请输入四个数字")
                return@setOnClickListener
            }
            setForUnique.clear()
            val numOne = numOneStr.toInt()
            val numTwo = numTwoStr.toInt()
            val numThree = numThreeStr.toInt()
            val numFour = numFourStr.toInt()
            if (!setForUnique.add(numOne) || !setForUnique.add(numTwo) ||
                    !setForUnique.add(numThree) || !setForUnique.add(numFour)) {
                TT.show("请输入四个不同数字")
                return@setOnClickListener
            }

            var numA: Int = 0
            var numB: Int = 0

            numList.forEachIndexed { index, num ->
                when (index) {
                    0 -> {
                        when (num) {
                            numOne -> numA++
                            numTwo -> numB++
                            numThree -> numB++
                            numFour -> numB++
                        }
                    }
                    1 -> {
                        when (num) {
                            numOne -> numB++
                            numTwo -> numA++
                            numThree -> numB++
                            numFour -> numB++
                        }
                    }
                    2 -> {
                        when (num) {
                            numOne -> numB++
                            numTwo -> numB++
                            numThree -> numA++
                            numFour -> numB++
                        }
                    }
                    3 -> {
                        when (num) {
                            numOne -> numB++
                            numTwo -> numB++
                            numThree -> numB++
                            numFour -> numA++
                        }
                    }
                }
            }

            sb.append(NumUtil.getNumCN(count++))
                    .append("、")
                    .append("${numA}A${numB}B   $numOne$numTwo$numThree$numFour")
                    .append("\n\n")
            tv_notes.text = sb.toString()

            if (numA == 4) {
                et_one.setText("恭")
                et_two.setText("喜")
                et_three.setText("过")
                et_four.setText("关")

                et_one.isFocusable = false
                et_two.isFocusable = false
                et_three.isFocusable = false
                et_four.isFocusable = false

                et_one.isEnabled = false
                et_two.isEnabled = false
                et_three.isEnabled = false
                et_four.isEnabled = false
                btn_commit.isEnabled = false
            } else {
                et_one.setText("")
                et_two.setText("")
                et_three.setText("")
                et_four.setText("")
            }
        }
    }

    private fun resetGame() {
        et_one.setText("")
        et_two.setText("")
        et_three.setText("")
        et_four.setText("")

        et_one.isFocusable = true
        et_two.isFocusable = true
        et_three.isFocusable = true
        et_four.isFocusable = true

        et_one.isEnabled = true
        et_two.isEnabled = true
        et_three.isEnabled = true
        et_four.isEnabled = true
        btn_commit.isEnabled = true

        et_one.requestFocus()

        count = 0
        tv_notes.text = ""
        randomNum()

        sb.delete(0, sb.length)

        // 测试输出结果
//        sb.append("${numList[0]}${numList[1]}${numList[2]}${numList[3]}")
//                .append("\n")
//        tv_notes.text = sb.toString()
    }

    private fun randomNum() {
        setForUnique.clear()
        val random = Random()
        numList.forEachIndexed { index, _ ->
            var num = random.nextInt(10)
            while (!setForUnique.add(num)) {
                num = random.nextInt(10)
            }
            numList[index] = num
        }
    }
}